package com.quartz.tasks.job;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.quartz.tasks.web.entity.HttpResultEntity;
import com.quartz.tasks.web.service.RestTemplateService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 彭长云
 * @Description:所有通过网络调用的任务都通过该方法
 * @date 2018/12/5 19:37
 */
public class TaskCenter implements Job, Serializable {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RestTemplateService restTemplate;

    @Override
    public void execute(JobExecutionContext jobExecutionContext){
        try {
            logger.info("任务开始:{}",jobExecutionContext);
            JobDataMap jobMap = jobExecutionContext.getTrigger().getJobDataMap();
            if(jobMap != null && jobMap.containsKey("url")){
                String url = (String) jobMap.get("url");
                String data = (String) jobMap.get("jsonData");
                HttpResultEntity result = restTemplate.post(url, data);
                logger.info("请求地址:{}",url);
                logger.info("请求参数:{}",data);
                logger.info("请求结果:{}",new ObjectMapper().writeValueAsString(result));
            }
            logger.info("任务结束:{}",jobExecutionContext);
            logger.info("-------------------------------------");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("",e.getMessage());
        }
    }
}
